using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Net;
using Xyne.Actions;
using Xyne.Regions;
using Xyne.Utilities;
using GarageGames.Torque.Core;
using Microsoft.Xna.Framework;
using GarageGames.Torque.T2D;

namespace Xyne.Templates.BattleEffects
{
    public class ThrownFireballEffect : BattleEffect
    {
        #region Constants
        private const string ObjectName = "ThrownObjectEffect";
        #endregion

        #region Members
        private ProjectileAction _thrownObjectAction;
        private BattleRegionImpl _battleRegion;
        private GameObjects _target;
        #endregion

        #region Properties
        #endregion

        #region Template
        private static ThrownFireballEffect _template;

        public static ThrownFireballEffect Template
        {
            get
            {
                if (_template == null)
                {
                    _template = new ThrownFireballEffect();
                }
                return _template;
            }
        }
        #endregion

        #region Constructor
        public ThrownFireballEffect()
        {
            IsTemplate = true;
            Name = ObjectName;

            CreateWithWorldLimit = true;
            
            AnimationData = AnimationManager.Instance.GetAnimationData("fireballANIM");
            Layer = Game.EffectLevel;
            ObjectType = TorqueObjectDatabase.Instance.GetObjectType(Name);
            
            Size = new Microsoft.Xna.Framework.Vector2(30, 30);
            EffectType = BattleEffectType.ThrownFireballEffect;

        }

        public void Init(ProjectileAction thrownObjectAction, BattleRegionImpl battleRegion, GameObjects target)
        {
            AnimationData.AnimationDuration = AnimationData.AnimationFrames.Length / thrownObjectAction.Speed *40;
            
            _thrownObjectAction = thrownObjectAction;
            _battleRegion = battleRegion;
            _target = target;


            
        }
        #endregion

        #region Overrides
        public override void StartEffect()
        {
            base.StartEffect();
            Vector2 targetPosition = _battleRegion.GetPosition(_target);
            Vector2 originPosition = _battleRegion.GetPosition(_thrownObjectAction.ActualOrigin);
            FlipX = targetPosition.X > originPosition.X;
            if (FlipX)
            {
                Physics.Velocity = new Vector2(_thrownObjectAction.Speed, 0);
            }
            else
            {
                Physics.Velocity = new Vector2(-_thrownObjectAction.Speed, 0);
            }
            WarpToPosition(originPosition,0.0f);
            CollisionsEnabled = true;
            Collision.CollidesWith = _battleRegion.GetTargetObjectType(_target);
            Collision.OnCollision = new  T2DOnCollisionDelegate(OnCollision);

            Init();
        }

        public void Init()
        {

            OnAnimationEnd = new OnAnimationEndDelegate(AnimationEnded);
        }
        #endregion
 
        #region Serialization
        public void Update(PacketReader reader)
        {
            Position = reader.ReadVector2();
            Physics.Velocity = reader.ReadVector2();
        }

        public override void Serialize(PacketWriter writer)
        {
            writer.Write(Position);
            writer.Write(Physics.Velocity);
        }
        #endregion

        #region Events
        public void OnCollision(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolveCollisionDelegate,
            ref T2DCollisionMaterial physicsMaterial)
        {
            _battleRegion.ModifyHealth(_target, _thrownObjectAction.HealthChange);
            ourObject.MarkForDelete = true;
            BattleEffectManager.Instance.RemoveBattleEffect(ourObject as BattleEffect);

            if (_thrownObjectAction.OnCollision != null && _thrownObjectAction.Level >= _thrownObjectAction.OnCollision.MinValue)
            {
                _thrownObjectAction.OnCollision.Action.Level = _thrownObjectAction.OnCollision.TriggeredValue;

                ActionManager.Instance.ActionActivated(_thrownObjectAction.OnCollision.Action);
            }
        }

        public void AnimationEnded()
        {
            SetAnimationFrame(0);
            ResumeAnimation();
        }
        #endregion
    }
}
